// api/auth.js
import CommonConstant from '../../utils/CommonConstant';

export function login() {
  return new Promise((resolve, reject) => {
    wx.request({
      url: "https://iam.cn-east-3.myhuaweicloud.com/v3/auth/tokens",
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        "auth": {
          "identity": {
            "methods": [
              "password"
            ],
            "password": {
              "user": {
                "domain": {
                  "name": "GT-2301_81266702"
                },
                "name": "GT-2301_81266702",
                "password": "Lmcindy6388"
              }
            }
          },
          "scope": {
            "project": {
              "name": "cn-east-3"
            }
          }
        }
      },
      success(res) {
        if (res.statusCode === 201) {
          const token = res.header['x-subject-token'];
          console.log("token=", token);
          CommonConstant.token = token;
          resolve(token);
        } else {
          reject(new Error("请求失败"));
        }
      },
      fail(err) {
        console.error("网络请求错误", err);
        reject(new Error("网络请求错误"));
      }
    });
  });
}